/*     */ package com.equipmentmanage.dao.impl;
/*     */ 
/*     */ import com.equipmentmanage.dao.UserDao;
import com.equipmentmanage.model.PageBean;
import com.equipmentmanage.model.User;
import com.equipmentmanage.util.StringUtil;

/*     */ import java.sql.ResultSet;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import javax.annotation.Resource;
/*     */ import org.springframework.jdbc.core.JdbcTemplate;
/*     */ import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;
/*     */ 
/*     */ @Repository("userDao")
/*     */ public class UserDaoImpl
/*     */   implements UserDao
/*     */ {
/*     */ 
/*     */   @Resource
/*     */   private JdbcTemplate jdbcTemplate;
/*     */ 
/*     */   public User login(User user)
/*     */   {
/*  32 */     String sql = "select * from t_user where userName=? and password=?";
/*  33 */     final User resultUser = new User();
/*  34 */     this.jdbcTemplate.query(sql, new Object[] { user.getUserName(), user.getPassword() }, new RowCallbackHandler()
/*     */     {
/*     */       public void processRow(ResultSet rs) throws SQLException
/*     */       {
/*  38 */         resultUser.setId(Integer.valueOf(rs.getInt("id")));
/*  39 */         resultUser.setUserName(rs.getString("userName"));
/*  40 */         resultUser.setPassword(rs.getString("password"));
/*  41 */         resultUser.setRoleName(rs.getString("roleName"));
/*     */       }
/*     */     });
/*  44 */     return resultUser;
/*     */   }
/*     */ 
/*     */   public void add(User user) {
/*  48 */     String sql = "insert into t_user values(null,?,?,?,?,?)";
/*  49 */     this.jdbcTemplate.update(sql, new Object[] { user.getUserName(), user.getPassword(), user.getTrueName(), user.getRoleName(), Integer.valueOf(user.getDeptId()) });
/*     */   }
/*     */ 
/*     */   public void update(User user) {
/*  53 */     String sql = "update t_user set userName=?,password=?,trueName=?,roleName=?,deptId=? where id=?";
/*  54 */     this.jdbcTemplate.update(sql, new Object[] { user.getUserName(), user.getPassword(), user.getTrueName(), user.getRoleName(), Integer.valueOf(user.getDeptId()), user.getId() });
/*     */   }
/*     */ 
/*     */   public void delete(int id) {
/*  58 */     String sql = "delete from t_user where id=?";
/*  59 */     this.jdbcTemplate.update(sql, new Object[] { Integer.valueOf(id) });
/*     */   }
/*     */ 
/*     */   public List<User> find(PageBean pageBean, User s_user) {
/*  63 */     StringBuffer sb = new StringBuffer("select * from t_user t1,t_department t2 where t1.deptId=t2.id");
/*  64 */     if ((s_user != null) && 
/*  65 */       (StringUtil.isNotEmpty(s_user.getUserName()))) {
/*  66 */       sb.append(" and t1.userName like '%" + s_user.getUserName() + "%'");
/*     */     }
/*     */ 
/*  69 */     if (pageBean != null) {
/*  70 */       sb.append(" limit " + pageBean.getStart() + "," + pageBean.getPageSize());
/*     */     }
/*  72 */     final List userList = new ArrayList();
/*  73 */     this.jdbcTemplate.query(sb.toString(), new Object[0], new RowCallbackHandler()
/*     */     {
/*     */       public void processRow(ResultSet rs) throws SQLException
/*     */       {
/*  77 */         User user = new User();
/*  78 */         user.setId(Integer.valueOf(rs.getInt("id")));
/*  79 */         user.setUserName(rs.getString("userName"));
/*  80 */         user.setPassword(rs.getString("password"));
/*  81 */         user.setTrueName(rs.getString("trueName"));
/*  82 */         user.setRoleName(rs.getString("roleName"));
/*  83 */         user.setDeptId(rs.getInt("deptId"));
/*  84 */         user.setDeptName(rs.getString("deptName"));
/*  85 */         userList.add(user);
/*     */       }
/*     */     });
/*  88 */     return userList;
/*     */   }
/*     */ 
/*     */   public int count(User s_user) {
/*  92 */     StringBuffer sb = new StringBuffer("select count(*) from t_user t1,t_department t2 where t1.deptId=t2.id");
/*  93 */     if ((s_user != null) && 
/*  94 */       (StringUtil.isNotEmpty(s_user.getUserName()))) {
/*  95 */       sb.append(" and t1.userName like '%" + s_user.getUserName() + "%'");
/*     */     }
/*     */ 
/*  98 */     return ((Integer)this.jdbcTemplate.queryForObject(sb.toString(), Integer.class)).intValue();
/*     */   }
/*     */ 
/*     */   public User loadById(int id) {
/* 102 */     String sql = "select * from t_user t1,t_department t2 where t1.deptId=t2.id and t1.id=? ";
/* 103 */     final User user = new User();
/* 104 */     this.jdbcTemplate.query(sql, new Object[] { Integer.valueOf(id) }, new RowCallbackHandler()
/*     */     {
/*     */       public void processRow(ResultSet rs) throws SQLException
/*     */       {
/* 108 */         user.setId(Integer.valueOf(rs.getInt("id")));
/* 109 */         user.setUserName(rs.getString("userName"));
/* 110 */         user.setPassword(rs.getString("password"));
/* 111 */         user.setTrueName(rs.getString("trueName"));
/* 112 */         user.setRoleName(rs.getString("roleName"));
/* 113 */         user.setDeptId(rs.getInt("deptId"));
/* 114 */         user.setDeptName(rs.getString("deptName"));
/*     */       }
/*     */     });
/* 117 */     return user;
/*     */   }
/*     */ 
/*     */   public boolean existUserByDeptId(int deptId) {
/* 121 */     StringBuffer sb = new StringBuffer("select count(*) from t_user where deptId=?");
/* 122 */     int result = ((Integer)this.jdbcTemplate.queryForObject(sb.toString(), new Object[] { Integer.valueOf(deptId) }, Integer.class)).intValue();
/* 123 */     if (result > 0) {
/* 124 */       return true;
/*     */     }
/* 126 */     return false;
/*     */   }
/*     */ }

/* Location:           C:\Users\arcgis\Desktop\新建文件夹\a0\[www.java1234.com]《SpringMvc设备管理系统》第零讲源码及文档\EquipmentSys\WEB-INF\classes\
 * Qualified Name:     com.java1234.dao.impl.UserDaoImpl
 * JD-Core Version:    0.6.1
 */